package com.laowang.controller;

import com.laowang.dataoject.ProductInfo;
import com.laowang.enums.PayStatusEnum;
import com.laowang.enums.ProductStatusEnum;
import com.laowang.enums.ResultEnum;
import com.laowang.exception.SellException;
import com.laowang.sercice.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;
import java.util.Optional;

/**
 * Created by Administrator on 2019/1/21.
 */
@Controller
@RequestMapping("/seller/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @GetMapping("list")
    public ModelAndView findAllProduct(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                       @RequestParam(value = "size",defaultValue = "2") Integer size,
                                       Map<String,Object> map){
        PageRequest pageRequest= new PageRequest(page-1,size);
        Page<ProductInfo> productInfoPage = productService.findAllProduct(pageRequest);
        map.put("productInfoPage",productInfoPage);
        map.put("currentPage",page);
        map.put("size",size);
        return new ModelAndView("product/list",map);

    }
    @GetMapping("/on_sale")
    public ModelAndView onSell(@RequestParam("productId") String productId,
                             Map<String,Object> map){
        try {
            productService.onSell(productId);
        }catch(Exception e){
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/product/list");
            return new ModelAndView("common/error",map);
        }
        map.put("url","/sell/seller/product/list");
        return new ModelAndView("common/success",map);
    }
    @GetMapping("/off_sale")
    public ModelAndView offSell(@RequestParam("productId") String productId,
                               Map<String,Object> map){

        try {
            productService.offSell(productId);
        }catch(Exception e){
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/product/list");
            return new ModelAndView("common/error",map);
        }
        map.put("url","/sell/seller/product/list");
        return new ModelAndView("common/success",map);
    }
}
